package com.lcy.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcy.reggie.common.Result;
import com.lcy.reggie.entity.AddressBook;
import com.lcy.reggie.service.AddressBookService;
import com.lcy.reggie.mapper.AddressBookMapper;
import com.lcy.reggie.utils.BaseContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
* @author ChunBaBa_
* @description 针对表【address_book(地址管理)】的数据库操作Service实现
* @createDate 2023-01-17 16:11:41
*/
@Transactional
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>
    implements AddressBookService{

    /**
     * 查看地址簿
     * @return
     */
    @Override
    public Result<List<AddressBook>> listAddressBook() {
        //1.通过threadLocal获取userId
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> addressBooks = this.query().eq("user_id",userId).orderByDesc("create_time").list();
        return Result.success(addressBooks);
    }

    /**
     * 新增收货地址
     * @param addressBook
     * @return
     */
    @Override
    public Result<String> saveAddress(AddressBook addressBook) {
        //1.通过threadLocal获取userId
        Long userId = BaseContext.getCurrentId();
        //2.将userId保存再addressBook
        addressBook.setUserId(userId);
        this.save(addressBook);
        return Result.success("保存成功！");
    }

    /**
     * 修改默认地址
     * @param addressBook
     * @return
     */
    @Override
    public Result<AddressBook> updateDefault(AddressBook addressBook) {
        //1.通过threadLocal获取userId
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> addressList = this.query().eq("user_id", userId).list();
        //2.将所有地址设置为非默认
        for (AddressBook address : addressList) {
            address.setIsDefault(0);
        }
        this.updateBatchById(addressList);
        addressBook.setIsDefault(1);
        this.updateById(addressBook);
        return Result.success(addressBook);
    }


    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override
    public Result<AddressBook> selectById(Long id) {
        if(id == null){
            return Result.error("地址不存在，请稍后再试！");
        }
        AddressBook address = this.getById(id);
        return Result.success(address);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @Override
    public Result<String> updateAddress(AddressBook addressBook) {
        //判断一下信息是否存在
        AddressBook address = this.query().allEq(new HashMap<String, Object>() {
            {
                put("consignee", addressBook.getConsignee());
                put("detail", addressBook.getDetail());
                put("label", addressBook.getLabel());
                put("phone", addressBook.getPhone());
                put("sex", addressBook.getSex());

            }
        }, true).one();
        if(address != null){
            return Result.error("收货地址已存在，请重新编辑！");
        }
        this.updateById(addressBook);
        return Result.success("修改成功！");
    }


}




